/**
 * Created by Administrator on 2015/6/12.
 */
'use strict';

var mysql = require('../common/mysql.js');
var M = require('./index.js');
var F = require('../common/function');

//帖子种类实体类
function threadCategory(category) {
    this.set(category);
}

threadCategory.prototype.get = function *(categoryid) {
    if (categoryid && categoryid > 0) {
        let result = yield mysql.queryOne('SELECT mtc.* FROM mh_thread_category mtc where mtc.cid=?', [categoryid]);
        if (result) {
            this.set(result);
        }
    }
    return this.categoryInfo;
};

threadCategory.prototype.set = function (categoryInfo) {
    this.cid = categoryInfo['cid'] || 0;
    this.name = categoryInfo['name'] || '';
    this.detail = categoryInfo['detail'] || '';
    this.pcid = categoryInfo['pcid'] || 0;
    this.threads = categoryInfo['threads'] || 0;
    this.posts = categoryInfo['posts'] || 0;
    this.views = categoryInfo['views'] || 0;
    this.status = categoryInfo['status'] || 0;
    this.sort = categoryInfo['sort'] || 0;
    this.ctype = categoryInfo['ctype'] || 0;
    this.pincount = categoryInfo['pincount'] || 0;
    this.categoryimg = categoryInfo['categoryimg'] || 0;
    this.sliderstr = categoryInfo['sliderstr'] || '';
    this.latest = categoryInfo['latest'] || 0;
    this.threadstatus=categoryInfo['threadstatus']||1;
};

//根据条件返回帖子类别列表
threadCategory.prototype.query = function*() {
    var findFields = [];
    var findVal = [];
    let strSQL = 'SELECT cid, name,detail,threads,status,sort,latest,threadstatus FROM mh_thread_category WHERE ';
    findFields.push('1=1');
    //findFields.push('status!=0');
    if (this.cid >= 1) {
        findFields.push('cid=?');
        findVal.push(this.cid);
    }
    if (this.name && this.name.length > 0) {
        findFields.push('name LIKE CONCAT("%",?,"%")');
        findVal.push(this.name);
    }
    if (this.ctype >= 0) {
        findFields.push('ctype=?');
        findVal.push(this.ctype);
    }
    if (this.status >= 0) {
        findFields.push('status=?');
        findVal.push(this.status);
    }
    if (this.latest >= 0) {
        findFields.push('latest=?');
        findVal.push(this.latest);
    }
    if (this.pcid >= 0) {
        findFields.push('pcid=?');
        findVal.push(this.pcid);
    }
    if(this.threadstatus>=0){
        findFields.push('threadstatus=?');
        findVal.push(this.threadstatus);
    }
    if (findFields.length > 0) {
        strSQL += findFields.join(' AND ');
    }

    if (this.pcid > 0) {
        strSQL += ' ORDER BY sort ASC';
    }

    let category = yield mysql.query(strSQL, findVal);
    return M.returnData(0, '获取成功', category);
};

//删除一个帖子类别
threadCategory.prototype.delete = function *() {
    if (this.cid && this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set status=0 where cid=?', [this.cid]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '删除失败');
        }
        else {
            return M.returnData(0, '删除成功');
        }
    } else {
        return M.returnData(1, '参数无效');
    }
};
//增加帖子类别发帖数
threadCategory.prototype.increaseThread = function*() {
    if (this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set threads=threads+1 where cid=?', [this.cid]);
        if (result.affectedRows > 0) {
            return M.returnData(0, '更新成功');
        } else {
            return M.returnData(1, '更新失败');
        }
    }
};

//减少帖子类别发帖数
threadCategory.prototype.decreaseThread = function*() {
    if (this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set threads=threads-1 where cid=?', [this.cid]);
        if (result.affectedRows > 0) {
            return M.returnData(0, '更新成功');
        } else {
            return M.returnData(1, '更新失败');
        }
    }
};

//增加帖子类别回帖数
threadCategory.prototype.increasePost = function*() {
    if (this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set posts=posts+1 where cid=?', [this.cid]);
        if (result.affectedRows > 0) {
            return M.returnData(0, '更新成功');
        } else {
            return M.returnData(1, '更新失败');
        }
    }
};

//减少帖子类别回帖数
threadCategory.prototype.decreasePost = function*() {
    if (this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set posts=posts-1 where cid=?', [this.cid]);
        if (result.affectedRows > 0) {
            return M.returnData(0, '更新成功');
        } else {
            return M.returnData(1, '更新失败');
        }
    }
};

//增加帖子类别浏览次数
threadCategory.prototype.increaseView = function*() {
    if (this.cid > 0) {
        let result = yield mysql.query('update mh_thread_category set views=views+1 where cid=?', [this.cid]);
        if (result.affectedRows > 0) {
            return M.returnData(0, '更新成功');
        } else {
            return M.returnData(1, '更新失败');
        }
    }
};

threadCategory.prototype.save = function *() {
    if (this.cid > 0) { // 帖子类别
        let result = yield mysql.query('UPDATE mh_thread_category SET ' +
            ' name=?,' +
            ' detail=?,' +
            ' pcid=?,' +
            ' status=?,' +
            ' latest=?,' +
            ' categoryimg=?,' +
            ' ctype=?,' +
            ' sort=?,' +
            ' threadstatus=?' +
            ' WHERE cid=?', [
            this.name,
            this.detail,
            this.pcid,
            this.status,
            this.latest,
            this.categoryimg,
            this.ctype,
            this.sort,
            this.threadstatus,
            this.cid
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '更新失败');
        }
        else {
            return M.returnData(0, '更新成功');
        }
    } else { // 新帖子类别
        let result = yield mysql.query('INSERT INTO mh_thread_category(' +
            'name,detail,pcid,status,latest,createtime,categoryimg,ctype,sort' +
            ') VALUES(?,?,?,?,?,UNIX_TIMESTAMP(),?,?,?)', [
            this.name,
            this.detail,
            this.pcid,
            this.status,
            this.latest,
            this.categoryimg,
            this.ctype,
            this.sort
        ]);
        if (result.affectedRows == 0) {
            return M.returnData(1, '保存失败');
        }
        else {
            this.aid = result.insertId;
            return M.returnData(0, '保存成功');
        }
    }
};
//自增排序
threadCategory.prototype.increaseSort = function *() {
    //判断有没有pcid，是否为二级类别
    let pcid = this.pcid;
    let sliderstr = this.sliderstr;
    if (pcid == '0') {
        return M.returnData(1, '只允许对二级类别操作.');
    }
    if (sliderstr == "") {
        return M.returnData(1, '没有排序的项.');

    }

    //找出数据库中的pcid对于的cid->db_cids
    let result = yield mysql.query('SELECT cid FROM mh_thread_category WHERE pcid=?', [pcid]);
    let db_cids = [];
    for (let i in result) {
        db_cids.push(result[i].cid);
    }
    //判断sliderstr是否在db_cids 中
    let req_cids = sliderstr.split(",");
    if (db_cids.length > 0) {
        let tempCids = ',' + db_cids.join(",") + ",";
        for (let i in req_cids) {
            if (tempCids.indexOf("," + req_cids[i] + ",") == -1) {
                return M.returnData(1, '参数有误.');
            }
        }
    }
    //修改数据库排序sort递增根据sliderstr
    let temp_db_cids = db_cids;
    for (let _i in req_cids) {
        let i = parseInt(_i);
        let temp = parseInt(i) + 1;
        mysql.query('UPDATE mh_thread_category set sort=?,status=1 WHERE cid=?', [temp, req_cids[i]]);
        //db_cids.remove(req_cids[i]);
        for (let _j in db_cids) {
            let j = parseInt(_j);
            if (db_cids[j] == req_cids[i]) {
                temp_db_cids.splice(j, 1)
            }
        }
    }


    //没有在sliderstr中的db_cids个别类别设为sort = 0 并 status = 0
    for (let i in temp_db_cids) {
        mysql.query('UPDATE mh_thread_category set sort=0,status=0 WHERE cid=?', [temp_db_cids[i]]);
    }
    //修改成功
    return M.returnData(0, '操作成功', result);
};

exports.threadCategory = threadCategory;